﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "UI/WidgetController/AttributeMenuWidgetController.h"

#include "StarrySkyGameplayTags.h"
#include "AbilitySystem/StarrySkyAttributeSet.h"

void UAttributeMenuWidgetController::BroadcastInitialValues()
{
	UStarrySkyAttributeSet* AS = CastChecked<UStarrySkyAttributeSet>(AttributeSet);
	check(AttributeInfo);
	//循环属性集中的游戏标签和属性的绑定关系Map
	for(auto& Pair:AS->TagsToAttributeSets)
	{
		//根据key游戏标签找到资产数据中属性结构体信息
		//FStarrySkyAttributeInfo Info=AttributeInfo->FindAttributeInfoByTag(Pair.Key);
		/**
		*Pair.Value.Execute()
		*Pair.Value 是一个 FAttributeSignature 委托
		*Execute() 调用委托绑定的函数，返回 FGameplayAttribute
		*这里会调用之前通过 BindStatic 绑定的属性获取函数（如 GetStrengthAttribute）.GetNumericValue(AS)
		*GetNumericValue 是 FGameplayAttribute 的方法
		*传入 UStarrySkyAttributeSet 实例 AS
		**/
		//Info.AttributeValue=Pair.Value.Execute().GetNumericValue(AS);
		//Info.AttributeValue=Pair.Value().GetNumericValue(AS);
		//StarrySkyAttributeInfoDelegate.Broadcast(Info);
		BroadcastAttributeInfo(Pair.Key,Pair.Value());
	}
}

void UAttributeMenuWidgetController::BindCallbacksToDependencies()
{
	check(AttributeInfo);
	UStarrySkyAttributeSet* AS = CastChecked<UStarrySkyAttributeSet>(AttributeSet);
	//循环创建属性变化时候的监听
	for(auto& Pair:AS->TagsToAttributeSets)
	{
		AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(Pair.Value()).AddLambda(
			[this,Pair](const FOnAttributeChangeData& Data)
		{
			BroadcastAttributeInfo(Pair.Key,Pair.Value());
		});
	}
}

void UAttributeMenuWidgetController::BroadcastAttributeInfo(const FGameplayTag& AttributeTag,
	const FGameplayAttribute& Attribute) const
{
	FStarrySkyAttributeInfo Info=AttributeInfo->FindAttributeInfoByTag(AttributeTag);
	Info.AttributeValue=Attribute.GetNumericValue(AttributeSet);
	StarrySkyAttributeInfoDelegate.Broadcast(Info);
}
